package com.zxl.yjb.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.TextView;

import com.zxl.yjb.R;
import com.zxl.yjb.activity.my.FillInfoActivity;
import com.zxl.yjb.app.Const;
import com.zxl.yjb.base.BaseActivity;
import com.zxl.yjb.bean.LoginSucessBean;
import com.zxl.yjb.http.HostUrl;
import com.zxl.yjb.utils.SPUtil;
import com.zxl.yjb.utils.SaveBeanUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.UserInfo;


/**
 * 引导页
 */
public class SplashActivity extends BaseActivity {

    @BindView(R.id.tv_version)
    TextView tvVersion;

    private SharedPreferences sharedPreferences;
    private String oldPrivince;
    private boolean isFirstLogin = false;
    private String ryToken;
    private String localVersion = "";
    private LoginSucessBean loginSucessBean;
    private String fillMyinfo = "";


    @Override
    protected void initView() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = getLayoutInflater().inflate(R.layout.activity_splash, null, false);
        setContentView(view);
        ButterKnife.bind(this);
        try {
            loginSucessBean = (LoginSucessBean) SaveBeanUtil.getBean(this,Const.VIPINFO);
            fillMyinfo = (String) SaveBeanUtil.getBean(this,Const.FILLMYINFO);
        } catch (Exception e) {
            e.printStackTrace();

        }

        try {
            PackageInfo packageInfo = getApplicationContext()
                    .getPackageManager()
                    .getPackageInfo(getPackageName(), 0);
            localVersion = packageInfo.versionName + "";
            tvVersion.setText("v"+localVersion);
        } catch (Exception e) {

        }


        isFirstLogin = (boolean) SPUtil.get(SplashActivity.this, Const.ISFIRST, false);
        if (loginSucessBean != null && !loginSucessBean.toString().equals("")) {
            ryToken = loginSucessBean.getRytoken();
        }
        openAnimation(view);//开启动画；
    }

    private void openAnimation(View view) {
        AlphaAnimation alphaAnimation = new AlphaAnimation(0.3f, 1.0f);
        alphaAnimation.setDuration(2000);
        view.startAnimation(alphaAnimation);
        alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationEnd(Animation arg0) {
                if (isFirstLogin) {
                    /*Intent intent = new Intent(SplashActivity.this, WelcomeActivity.class);
                    startActivity(intent);*/
                    login();
                } else {
                    login();
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationStart(Animation animation) {
            }

        });
    }

    private void login() {
        if (!TextUtils.isEmpty(ryToken)) {
            if (fillMyinfo!=null&&fillMyinfo.equals("1"))
            {
                Intent intent = new Intent(SplashActivity.this, FillInfoActivity.class);
                startActivity(intent);
            }
            else
            {
                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(intent);
            }

            loginIM();//登陆Im；
        } else {
            Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
            startActivity(intent);
        }
        finish();
    }

    private void loginIM() {
        try {
//                SealAppContext instance = SealAppContext.getInstance();
//                RongIMClient.ConnectCallback connectCallback = instance.getConnectCallback();
            RongIMClient.ConnectCallback connectCallback = new RongIMClient.ConnectCallback() {
                @Override
                public void onSuccess(String s) {
                    SharedPreferences sp = getSharedPreferences(Const.SHAREDPREFERENCES_FILE_NAME, Context.MODE_PRIVATE);
                    sp.edit().putString(Const.SEALTALK_LOGIN_ID, s).commit();
                    Object bean = SaveBeanUtil.getBean(getApplication(), Const.VIPINFO);
                    if (bean != null) {
                        LoginSucessBean loginSucessBean = (LoginSucessBean) bean;
                        RongIM.getInstance().setCurrentUserInfo(new UserInfo(s,
                                loginSucessBean.getNickname(),
                                Uri.parse(HostUrl.HOSTPIC + loginSucessBean.getFace_img())));
                    }
                }

                @Override
                public void onError(RongIMClient.ErrorCode errorCode) {

                }

                @Override
                public void onTokenIncorrect() {

                }
            };
            RongIM.connect(ryToken, connectCallback);
            RongIM.connect(ryToken, connectCallback);

        } catch (Exception e) {
            finish();
        }
    }


    /**
     * 页面跳转
     */
/*    private void redirectTo() {
        LoginSucessBean loginSucessBean = (LoginSucessBean) SaveBeanUtil.getBean(this, "vipInfo");
 if (loginSucessBean != null)
            oldPrivince = loginSucessBean.getOldProvince();
        Intent intent = new Intent();
        if (TextUtils.isEmpty(token)) {//未登录
            intent.setClass(this, LoginActivity.class);
        } else {
            if (TextUtils.isEmpty(oldPrivince)) {//未填写籍贯 工作城市等信息  需要重新登录
                intent.setClass(this, LoginActivity.class);
            } else {//已登录 并且 已填写信息
                intent.setClass(this, MainActivity.class);
            }
        }
        startActivity(intent);
        finish();
    }*/

}
